#!/bin/bash
# Usage : ProgramName [list of target sample names]

# Job name headder
JOBHEAD="DEF_"

# get file list as parameters
declare -a SAMPLES
SAMPLES=("$@")

for SampleName in ${SAMPLES[@]}
do
	JOB_FILE=${JOBHEAD}${SampleName}.sh

	# <<- ignore tab in the head line in here document
	cat <<- 'EOF' > ${JOB_FILE}
	#$ -cwd
	#$ -j y
	#$ -S /bin/bash
	#$ -M rafysta@gmail.com
	#$ -m es


	export PATH=/cscb/cscbsoftware/centos5-x86_64/local/bin:$PATH
	export PATH=$PATH:/usr/local/bin
	export PATH=/home/hideki/bio/MACS-1.4.2/bin:$PATH
	export Program=/cscb/noma_nfs/Project/2014-04-30_ChIA-PET_rad21_biological_replica/bin
	export Data=/cscb/noma_nfs/Data/2014-04-30_ChIA-PET_rad21_biological_replica
EOF

	echo "export SampleName=${SampleName}" >> ${JOB_FILE}

	cat <<- 'EOF' >> ${JOB_FILE}
	#perl $Program/Filter_read_by_repeat.pl -i $Data/${SampleName}_linker_sort_PCR.map -o $Data/${SampleName}_linker_sort_PCR_repeat.map
	#perl $Program/Filter_read_by_selfligation.pl -i $Data/${SampleName}_linker_sort_PCR_repeat.map -o $Data/${SampleName}_linker_sort_PCR_repeat_self.map
	#perl $Program/Connect_association_to_binding_siteName.pl -i $Data/${SampleName}_linker_sort_PCR_repeat_self.map -r $Data/${SampleName}_summits.bed -o $Data/${SampleName}_linker_sort_PCR_repeat_self_bind.map
	#perl $Program/Make_map_file_random.pl -i $Data/${SampleName}_linker_sort_PCR_repeat_self_bind.map -o $Data/${SampleName}_random.map
	#perl $Program/Make_associationFile.pl -i $Data/${SampleName}_linker_sort_PCR_repeat_self_bind.map -o $Data/${SampleName}.aso
	#perl $Program/Make_associationFile.pl -i $Data/${SampleName}_random.map -o $Data/${SampleName}_random.aso
	Rscript $Program/Pvalue_calculation.R $Data/${SampleName}.aso $Data/${SampleName}_pval.aso
	Rscript $Program/Pvalue_calculation.R $Data/${SampleName}_random.aso $Data/${SampleName}_random_pval.aso
	Rscript $Program/FDR_filter.R $Data/${SampleName}_pval.aso $Data/${SampleName}_random_pval.aso $Data/${SampleName}_pval_FDR.aso
EOF

	qsub -pe smp 1 ${JOB_FILE}
done
